package my.code.charpter5;

public class BucketState {

	private static int[] bucket_capacity = new int[]{8, 5, 3};
	private int[] bucket_s;	//依次表示8升桶水量、5升桶水量、3升桶水量，长度必须是3，简单demo，这里就不校验了
	private TagAction curAction;
	
	public BucketState() {
	}
	
	public BucketState(int[] bs, TagAction action) {
		this.bucket_s = bs;
		this.curAction = action;
	}
	
	public boolean isFinish(){
		if(bucket_s[0] == 4 && bucket_s[1] == 4){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean canTakeDumpAction(int from, int to){
		if(from != to && !IsBucketEmpty(from) && !IsBucketFull(to)){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean IsBucketEmpty(int bucketNo){
		if(bucket_s[bucketNo] == 0){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean IsBucketFull(int bucketNo){
		if(bucket_s[bucketNo] == bucket_capacity[bucketNo]){
			return true;
		}else{
			return false;
		}
	}
	
	public void DumpWater(int from, int to, BucketState next){
		int[] next_bucket_s = new int[]{bucket_s[0], bucket_s[1], bucket_s[2]};
		
		int from_w = bucket_s[from];
		int to_w = bucket_s[to];
		int to_c = bucket_capacity[to];
		int dumpWater = to_c - to_w > from_w ? from_w : to_c - to_w;
		
		next_bucket_s[from] -= dumpWater;
		next_bucket_s[to] += dumpWater;
		
		next.setBucket_s(next_bucket_s);
		next.setCurAction(new TagAction(from, to, dumpWater));
	}
	
	public int[] getBucket_s() {
		return bucket_s;
	}

	public void setBucket_s(int[] bucket_s) {
		this.bucket_s = bucket_s;
	}

	public TagAction getCurAction() {
		return curAction;
	}

	public void setCurAction(TagAction curAction) {
		this.curAction = curAction;
	}

	public boolean isSameState(BucketState bs){
		int[] bucket_s_ = bs.getBucket_s();
		if(bucket_s[0] == bucket_s_[0]
				&& bucket_s[1] == bucket_s_[1]
				&& bucket_s[2] == bucket_s_[2]){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		if(curAction != null){
			sb.append(curAction);
			sb.append(" => ");
		}
		sb.append("[");
		sb.append(bucket_s[0]).append(",");
		sb.append(bucket_s[1]).append(",");
		sb.append(bucket_s[2]);
		sb.append("]");
		return sb.toString();
	}

}
